package com.ymm.platform.dao.mq.sender;

import com.alibaba.fastjson.JSON;
import com.ymm.platform.api.utils.ComplaintOpsEnum;
import com.ymm.platform.dao.mq.msg.ShipperComplaintsCompleteOnMsg;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @Description: TODO
 * @Author: Xuecheng Li
 * @CreateTime: 2022-08-17 20:51
 * @Version: 1.0.0
 */
@Component
public class ShipperComplaintsCompleteMsgSender {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${mq.topic.shipper.complaints}")
    private String topic;

    public void send(Long complaintsId, Long shipperId, Long orderId, Integer opsPersonType, Long opsPersonId) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        ShipperComplaintsCompleteOnMsg shipperCargoPutOnMsg = new ShipperComplaintsCompleteOnMsg();
        shipperCargoPutOnMsg.setComplaintsId(complaintsId);
        shipperCargoPutOnMsg.setComplaintId(shipperId);
        shipperCargoPutOnMsg.setOrderId(orderId);
        shipperCargoPutOnMsg.setOpsPersonType(opsPersonType);
        shipperCargoPutOnMsg.setOpsPersonId(opsPersonId);
        String keys = complaintsId + "|" + ComplaintOpsEnum.COMPLETE.getCode();
        sendMessage(topic, "complete", keys, JSON.toJSONString(shipperCargoPutOnMsg));
    }

    private void sendMessage(String topic, String tag, String keys, String body) throws MQBrokerException, RemotingException, InterruptedException, MQClientException, RemotingException, MQClientException {
        Message message = new Message(topic, tag, keys, body.getBytes(StandardCharsets.UTF_8));
        SendResult send = rocketMQTemplate.getProducer().send(message);
    }
}
